// 导入数据库API
const db = require('../../config/db');
// 操作数据库的业务
module.exports = {
    // 示例：
    test() {
        return new Promise((resolve, reject) => { })
    },
    // 记录成绩
    add(user_name, score_id, score_val, score_exam_time, exam_options, exam_time, exam_type, topic_lib_id, user_id, exam_id, exam_name, exam_fullcredit, exam_passline) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_score(user_name,score_id,score_val,score_exam_time,exam_options,exam_time,exam_type,topic_lib_id,user_id,exam_id,exam_name,exam_fullcredit,exam_passline) values("${user_name}","${score_id}",${score_val},${score_exam_time},"${exam_options}",${exam_time},"${exam_type}","${topic_lib_id}","${user_id}","${exam_id}","${exam_name}",${exam_fullcredit},${exam_passline})`;
            db.query(sql, (err, rows) => {
                console.log({err})
                if (err) {
                    reject({
                        code: -1,
                        msg: '发布成绩失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '发布成绩成功。'
                    })
                }
            })
        })
    },

    // 删除成绩（用户自己删除）
    remove(score_id, user_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_score where score_id = "${score_id}" and user_id="${user_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: '删除成绩失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '删除成绩成功。'
                    })
                }
            });
        })
    },

    // 删除成绩（管理员删除）
    delete(score_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_score where score_id = "${score_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: '删除成绩失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '删除成绩成功。'
                    })
                }
            });
        })
    },

    // 成绩列表
    list(page_num, page_size, user_id) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let len = page_size;
            let sql = `select * from db_score where user_id = "${user_id}" limit ${startIndex},${len}`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取成绩列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取成绩列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        }
                    })
                }
            })
        })

    },

    // 管理员
    list2(page_num, page_size) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let len = page_size;
            let sql = "select * from db_score limit " + startIndex + "," + len + "";
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取成绩列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取成绩列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        }
                    })
                }
            })
        })

    },

    // 成绩详情
    detail(score_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_score where score_id="${score_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询成绩列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "查询成绩详情成功。",
                        result: rows
                    })
                }
            })
        })
    },

    // 搜索成绩
    // 参数：keyword
    search(keyword) {
        return new Promise((resolve, reject) => {
            let sql = "select * from db_score where user_name LIKE CONCAT('%" + keyword + "%') or exam_name LIKE CONCAT('%" + keyword + "%')";
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询成绩信息失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: -1,
                            msg: '没有该关键字匹配成绩信息。'
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询成绩信息成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 统计成绩记录总数量
    total() {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_score', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计成绩数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取成绩数量成功。",
                        total: rows[0]['total']
                    })
                }
            })

        })
    },

    // 检查是否已经作答
    partake(topic_lib_id, user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_score where score_id="${topic_lib_id}" and user_id="${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '检查是否已经作答失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "已经参该考试了。",
                        result: rows
                    })
                }
            })
        })
    },

}